<template>
  <el-form :model="form" class="login-ruleForm">
    <el-form-item prop="name">
      <el-input :placeholder="t('register.username')" v-model="form.name">
        <template #prefix>
          <icon-user theme="outline" size="16" fill="#999" />
        </template>
      </el-input>
    </el-form-item>
    <!--<el-form-item>-->
      <!--<div class="form-code">-->
        <!--<el-input :placeholder="t('register.smsCode')" v-model="form.name">-->
          <!--<template #prefix>-->
            <!--<icon-user theme="outline" size="16" fill="#999" />-->
          <!--</template>-->
        <!--</el-input>-->
        <!--<el-button type="primary" class="code-btn">{{ t('register.smsbtn') }}</el-button>-->
      <!--</div>-->
    <!--</el-form-item>-->
    <el-form-item prop="password">
      <el-input :placeholder="t('register.password')" type="password" v-model="form.password">
        <template #prefix>
          <icon-lock theme="outline" size="16" fill="#999" />
        </template>
      </el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input :placeholder="t('register.confirmPwd')" type="password" v-model="form.confirmPwd">
        <template #prefix>
          <icon-lock theme="outline" size="16" fill="#999" />
        </template>
      </el-input>
    </el-form-item>
    <el-form-item>
      <div class="login-check">
        <el-checkbox v-model="checkedPwd">{{ t('register.checkText') }}</el-checkbox>
      </div>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" class="login-btn" round @click="handleRegister">{{ t('register.registerBtn') }}</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
  import { RegisterSubmit } from '@/api/user'

  import { ElMessageBox, ElMessage } from 'element-plus';
  import { ref,reactive,defineEmits,defineProps } from 'vue';
  import { useI18n } from 'vue-i18n';
  const { t } = useI18n();

  // const emit = defineEmits(['SelechChange', "update:value"]);

  const form = reactive({
    name: '',
    password: '',
    confirmPwd: ''
  });
  const checkedPwd=ref(false);

  let props = defineProps({
    activeName:{                            // 父组件v-model绑定的值
      type:String,
    }
  });

  // 2、定义发射给父组件的方法
  const emits = defineEmits(['handleClick', 'update:activeName'])

  const handleRegister = () => {

    if(!checkedPwd.value){
      ElMessage.error(`同意协议后方能创建用户！`);
    }else if(form.password!=form.confirmPwd){
      ElMessage.error(`确认密码必须与密码一致`);
    }else {
      RegisterSubmit(form).then(res=>{
        if(res.code==200){
          ElMessage.success("注册账户成功，请找管理员配置提升角色");
          emits('update:activeName','first');
        }else{
          ElMessage.error(res.message);
        }
      })
    }

  };

</script>

<style lang="scss" scoped>
  .login-ruleForm {
    margin-top: 1rem;
    :deep(.el-input__prefix) {
      top: 2px;
      padding: 0 4px;
    }
    .login-methods {
      display: flex;
      align-items: center;
      justify-content: space-around;
    }

    .login-btn {
      width: 100%;
      margin-bottom: 1rem;
    }
    .login-check {
      display: flex;
      align-content: center;
      justify-content: space-between;
    }
    .form-code {
      display: flex;
      align-content: center;
      justify-content: space-between;
      .code-btn {
        margin-left: 1rem;
      }
    }
  }
</style>
